package com.wbao.dianniu.update;

import android.content.Context;
import android.util.Log;

import com.mob.MobSDK;
import com.wbao.dianniu.R;
import com.wbao.dianniu.logical.GlobalContext;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;


public class ShareSDKManager {

	private static ShareSDKManager instance;
	private final String APP_KEY = "1475bc40e11d8";
	private final String APP_SECRETS = "0400de1ade4700e112b21b92e24b9c40";
	private static String SERVICE_URL = "http://share.dianniuapp.com/";
//	private static String SERVICE_URL = "http://118.178.189.156:8080/dianniu/share/";

	private ShareSDKManager() {
	}

	public static ShareSDKManager getInstance() {
		if (null == instance)
			instance = new ShareSDKManager();
		return instance;
	}

	/**
	 * @param context
	 * @param content	分享内容
	 * @param type 1： 问答分享 2：名片分享 3：app分享 ,4：求职分享 5： 招聘分享 6：活动分享，7：匿名分享
	 * @param imageUrl  图片：type=1 有问答图片显示图片，没有图片显示头像
	 * 												 type=2 显示头像
	 * @param id 问题id，用户Id，求职id，招聘id，活动id
	 */
	public void showShare(Context context, String content,int type,String imageUrl,int id) {
//		ShareSDK.initSDK(context);
		MobSDK.init(context, APP_KEY, APP_SECRETS);
		OnekeyShare oks = new OnekeyShare();
		oks.disableSSOWhenAuthorize();
		String url = SERVICE_URL;
		if(1 == type){
			oks.setTitle("来自电纽的问答分享");
			oks.setImageUrl(imageUrl);
			url += "quest.html?id=" + id;
		}else if(2 == type){
			oks.setTitle("来自电纽的名片分享");
			oks.setImageUrl(imageUrl);
			url += "card.html?id=" + id;
		}else if(3 == type){
			oks.setTitle("来自电纽的应用分享");
			oks.setImageUrl(imageUrl);
			url = "http://www.dianniu.me/dianniu/share/invite.html?u=" + GlobalContext.getAccountId();
		}else if(4 == type){
			oks.setTitle("来自电纽的职位分享");
			oks.setImageUrl(imageUrl);
			url += "job.html?id=" + id;
		}else if(5 == type){
			oks.setTitle("来自电纽的招聘分享");
			oks.setImageUrl(imageUrl);
			url  += "recruit.html?id=" + id;
		}else if(6 == type){
			oks.setTitle("来自电纽的活动分享");
			oks.setImageUrl(imageUrl);
			url  += "activity.html?id=" + id;
		}else if(7 == type){
			oks.setTitle("来自电纽的匿名分享");
			oks.setImageUrl(imageUrl);
			url += "aliasQuest.html?id=" + id;
		}else if(8 == type){
			oks.setTitle("来自电纽的知识分享");
			oks.setImageUrl(imageUrl);
			url += "knowledge.html?id=" + id;
		}
		oks.setTitleUrl(url);
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl(url);
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		oks.setSiteUrl(url);
		oks.setText(content);
//		oks.setComment("我正在使用电纽App，大家赶快加入吧！");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(context.getResources().getString(R.string.app_name));
//		oks.setCallback(listener);
		// 启动分享GUI
		oks.show(context);
		
	}

	PlatformActionListener listener = new PlatformActionListener() {
		
		@Override
		public void onError(Platform arg0, int arg1, Throwable arg2) {
			// TODO Auto-generated method stub
			if(arg1 == Platform.ACTION_SHARE){
//				UIHandler.sendEmptyMessage(SHARE_ERROR, this);
			}
		}
		
		@Override
		public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
			// TODO Auto-generated method stub
			if(arg1 == Platform.ACTION_SHARE){
//				UIHandler.sendEmptyMessage(SHARE_SUCCESS, this);
				Log.i("", "响应分享事件");
			}
			if(arg1 == Platform.SHARE_TEXT){
				Log.i("", "响应分享文本事件");
//				UIHandler.sendEmptyMessage(SHARE_SUCCESS, this);
				Log.i("", "..");
			}
		}
		
		@Override
		public void onCancel(Platform arg0, int arg1) {
			if(arg1 == Platform.ACTION_SHARE){
//				UIHandler.sendEmptyMessage(SHARE_CANCEL, this);
			}
		}
	};
	
	
	/**
	 * 分享到QQ好友
	 * 
	 */
	public void showShareToQQ(Context context, String content,String imageUrl,int id){
		     
		QQ.ShareParams sp = new QQ.ShareParams();
		sp.setTitle("电商交流，我在电纽等你！");
		String url = SERVICE_URL + "card.html?id=" + id;
		sp.setTitleUrl(url);
		sp.setImageUrl(imageUrl);
		sp.setText(content);
		Platform platform = ShareSDK.getPlatform(QQ.NAME);
		platform.setPlatformActionListener(null); // 设置分享事件回调
		// 执行图文分享
		platform.share(sp);
	}
	
	public void showShareToWechat(Context context, String content,String imageUrl,int id){
		Wechat.ShareParams sp = new Wechat.ShareParams();
		sp.setShareType(Platform.SHARE_WEBPAGE);
		sp.setTitle("电商交流，我在电纽等你！");
		sp.setText(content);
		sp.setImagePath(imageUrl);
		String url = SERVICE_URL + "card.html?id=" + id;
		// url仅在微信（包括好友和朋友圈）中使用
		sp.setUrl(url);
		Platform platform = ShareSDK.getPlatform(Wechat.NAME);
		platform.setPlatformActionListener(null); // 设置分享事件回调
		// 执行图文分享
		platform.share(sp);
	}
	
	public void showShareToBook(){
		
	}
	
	public void shareApp(Context context,int accountId){
//		ShareSDK.initSDK(context);
		MobSDK.init(context, APP_KEY, APP_SECRETS);
		OnekeyShare oks = new OnekeyShare();
		//关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setTitle("电纽,百万电商人都在用的社交APP");
		// text是分享文本，所有平台都需要这个字段
		oks.setText(context.getResources().getString(R.string.share_desc));
		String url = "http://www.dianniu.me/dianniu/share/invite.html?u=" + accountId;
		// titleUrl是标题的网络链接，QQ和QQ空间等使用
		oks.setTitleUrl(url);
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		oks.setSiteUrl(url);
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl(url);
		oks.setSilent(true);
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite("电纽");
		oks.setImageUrl(GlobalContext.cdndownUrl + "logo.png");
		oks.show(context);

	}


}
